//
//  JXLHomeTableViewCell.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/24.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

/*
 1. 原创微博视图
 2. 转发微博视图
 3. 底部toolBar视图
 
 */

import UIKit

//  cell子控件之间的内容间距
let JXLHomeTableViewCellMargin: CGFloat = 10


class JXLHomeTableViewCell: UITableViewCell {

    //纪录toolBar视图的顶部约束
    var toolBarTopConstrait: Constraint?
    
    //cell需要使用viewModel进行数据展示
    var statusViewModel: JXLStatusViewModel? {
        didSet {
            //绑定OriginalView的数据
            OriginalView.statusViewModel = statusViewModel
            
            //移除掉toolBarTopConstrait
            toolBarTopConstrait?.uninstall()
            
            if statusViewModel?.status?.retweeted_status != nil {
                retweetView.isHidden = false
                
                toolBarView.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstrait = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                retweetView.statusViewModel = statusViewModel
            } else {
                //没有转发微博
                retweetView.isHidden = true
                toolBarView.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstrait = make.top.equalTo(OriginalView.snp_bottom).constraint
                })
            }
            
            //绑定toolBarView的数据
            toolBarView.statusViewModel = statusViewModel
            
//        print(statusViewModel?.status?.pic_urls?.first?.thumbnail_pic)
        }
    }
    
    //微博原创视图
    lazy var OriginalView: JXLStatusOriginalView = {
        let view = JXLStatusOriginalView()
        
        return view
    }()
    
    //底部toolBarView
    lazy var toolBarView:JXLStatusToolBarView = JXLStatusToolBarView()
    //转发微博视图
    lazy var retweetView: JXLStatusRetweetView = {
        let view = JXLStatusRetweetView()
        
        return view
    }()
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置子控件
   private func setupUI() {
    //设置背景颜色
    contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)

    
    //添加子控价
    contentView.addSubview(OriginalView)
    contentView.addSubview(toolBarView)
    contentView.addSubview(retweetView)
    
    //布局OriginalView
    OriginalView.snp_makeConstraints { (make) in
        make.top.leading.trailing.equalTo(contentView)
        //make.height.equalTo(100)
    }
    
    //布局retweetView
    retweetView.snp_makeConstraints { (make) in
        make.top.equalTo(OriginalView.snp_bottom)
        make.leading.trailing.equalTo(OriginalView)
    }

    //布局toolBarView
    toolBarView.snp_makeConstraints { (make) in
        toolBarTopConstrait = make.top.equalTo(retweetView.snp_bottom).constraint
        make.leading.trailing.equalTo(retweetView)
        make.height.equalTo(35)
        
        //关键约束
        make.bottom.equalTo(contentView.snp_bottom)

    }
    
    
    
    }

}
